package com.whpost.admin.config;

import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.thymeleaf.spring6.view.ThymeleafViewResolver;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class ViewResolverConfig {

    @Resource
    private Environment env;

    @Value("${wdpz.api}")
    private String serverUrl;

    @Value("${wdpz.admin}")
    private String adminUrl;

    @Value("${wdpz.loginTitle}")
    private String loginTitle;

    @Resource
    @Qualifier("thymeleafViewResolver")
    private ThymeleafViewResolver thymeleafViewResolver;

    @PostConstruct
    public void configureThymeleafStaticVars() {
        if (thymeleafViewResolver != null) {
            Map<String, Object> vars = new HashMap<>();
            vars.put("webName", loginTitle);
            vars.put("loginTitle", loginTitle);
            vars.put("webVersion", "1.0.0");
            vars.put("serverUrl", serverUrl);
            vars.put("adminUrl", adminUrl);
            thymeleafViewResolver.setStaticVariables(vars);
        }
    }
}
